<template>
  <div>
    <van-address-edit
      :area-list="areaList"
      :address-info="search[0]"
      show-delete
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
    >
     <div class="van-cell van-field">
        <div class="van-cell__title van-field__label"><span>标签</span></div>
        <div class="van-cell__value van-field__value">
          <div class="van-field__body">
            <van-radio-group v-model="label" direction="horizontal">
              <van-radio name="家">家</van-radio>
              <van-radio name="公司">公司</van-radio>
              <van-radio name="学校">学校</van-radio>
            </van-radio-group>
          </div>
        </div>
      </div>
    </van-address-edit>
  </div>
</template>

<script>
import { Toast } from "vant";
import { areaList } from "@vant/area-data";
import { reqAddSite, reqDelSite, reqSitelist ,reqRessSite} from "../../http/http";

export default {
  data() {
    return {
      areaList,
      search: [],
      label: "家",
      searchResult: [],
    };
  },
  mounted() {
    if (this.$route.query.site == 2) {
      reqSitelist({
        uid: JSON.parse(localStorage.getItem("user")).uid,
        id: this.$route.query.id,
      }).then((res) => {
        if (res.data.code == 200) {
          this.search = res.data.list ? res.data.list : [];
          this.search = this.search.map((item) => {
            return {
              id: item.id,
              name: item.username,
              tel: item.userphone,
              areaCode:item.regioncode,
              addressDetail:item.useraddress,
              isDefault: item.status == 1 ? true : false,
            };
          });
          console.log(this.search);
        }
      });
    }
  },
  methods: {
    onSave(site) {
      console.log(site);
      if (this.$route.query.site == 1) {
       this.addsite(site);
      } else {
        this.resssite(site);
      }
    },
    onDelete() {
      if (this.$route.query.site == 1) {
        Toast("当前是新增页面,无法执行删除");
        return;
      }
      reqDelSite({
        id: this.$route.query.id,
      }).then((res) => {
        if (res.data.code == 200) {
          Toast("删除成功");
          this.$router.replace("/site");
        }
      });
    },
    addsite(site) {
      reqAddSite({
        //uid
        uid: JSON.parse(localStorage.getItem("user")).uid,
        //姓名
        username: site.name,
        //地区码
        regioncode: site.areaCode,
        //电话
        userphone: site.tel,
        //地区
        location: site.city+site.country+site.county,
        //地区详情
        useraddress: site.addressDetail,
        // 是否默认地址
        status: site.isDefault ? 1 : 0,

        label: this.label,
      }).then((res) => {
        if (res.data.code == 200) {
          Toast(res.data.msg);
          this.$router.replace("/site");
        }
      });
    },
    resssite(site) {
      reqRessSite({
        id:this.$route.query.id,
        //姓名
        username: site.name,
        //地区码
        regioncode: site.areaCode,
        //电话
        userphone: site.tel,
        //地区
        location: `${site.city}${site.country}${site.county}`,
        //地区详情
        useraddress: site.addressDetail,
        // 是否默认地址
        status: site.isDefault ? 1 : 0,
        label: this.label,

      }).then(res=>{
          if (res.data.code == 200) {
          Toast(res.data.msg);
          this.$router.replace("/site");
        }
      });
    },
  },
};
</script>

<style></style>
